package com.tannm.doan.taskmanager.dataview;

import java.util.*;

import com.tannm.doan.taskmanager.Prefs;
import com.tannm.doan.taskmanager.agenda.AgendaView;
import com.tannm.doan.taskmanager.database.Database;

import android.database.*;
import android.database.sqlite.*;

public abstract class DataView {
	// fields
	private final String sQuery = "select * from %s";
	protected Database db = null;
	protected Prefs prefs = null;
	protected String sTableName = "";
	protected ArrayList<DataViewItem> rows = new ArrayList<DataViewItem>();
	private Calendar calViewStartDate = Calendar.getInstance();

	// methods
	public DataView(Database db, Prefs prefs) {
		this.db = db;
		this.prefs = prefs;
	}

	protected abstract void addItem(Cursor cr);

	protected abstract void filterDataPrepare(final Calendar calStartDate,
			final int agendaViewType);

	protected abstract void filterDataForView(DataViewItem item,
			final Calendar calStartDate, final int agendaViewType);

	protected abstract void sortView();

	public boolean reloadTable() {
		// get data
		Database.Result result = collectRowsData();
		if (result == Database.Result.Success)
			return true;
		return false;
	}

	public Database.Result collectRowsData() {
		Database.Result result = Database.Result.errUnknown;
		rows.clear();
		String sql = String.format(sQuery, sTableName);
		SQLiteDatabase sqldb = db.getSQLiteDb();
		Cursor cr = sqldb.rawQuery(sql, null);
		if (cr == null) {
			result = Database.Result.errCantGetData;
		} else {
			if (cr.getCount() > 0) {
				cr.moveToFirst();
				while (!cr.isAfterLast()) {
					try {
						addItem(cr);
						result = Database.Result.Success;
					} catch (Exception e) {
						return Database.Result.errCantGetDataFromTable;
					}
					cr.moveToNext();
				}
			} else {
				result = Database.Result.errCantFindData;
			}
			sortView();
		}
		return result;
	}

	public DataViewItem getRow(int index, final int agendaViewType) {
		try {
			DataViewItem values = rows.get(index);
			if (agendaViewType == values.viewMode)
				return values;
		} catch (IndexOutOfBoundsException e) {
		}
		return null;
	}

	public int getRowsCountForView(final int agendaViewType) {
		int iCount = 0;
		for (int i = 0; i < rows.size(); i++)
			if (rows.get(i).viewMode == agendaViewType)
				iCount++;
		return iCount;
	}

	public int getRowsCountTotal() {
		return rows.size();
	}

	public int getDaysRangeForView(final int agendaViewType) {
		if (agendaViewType == AgendaView.viewMode.TODAY)
			return 0;

		if (agendaViewType == AgendaView.viewMode.DAY)
			return 0;

		if (agendaViewType == AgendaView.viewMode.WEEK)
			return 7;

		if (agendaViewType == AgendaView.viewMode.MONTH)
			return 42;

		// counting days backward !!!
		if (agendaViewType == AgendaView.viewMode.TODAY_ALARM)
			return 7;

		return 0;
	}

	public void filterData(final Calendar calStartDate, final int agendaViewType) {
		calViewStartDate.setTimeInMillis(calStartDate.getTimeInMillis());
		calViewStartDate.setFirstDayOfWeek(prefs.iFirstDayOfWeek);

		filterDataPrepare(calViewStartDate, agendaViewType);

		for (int i = 0; i < rows.size(); i++) {
			DataViewItem item = rows.get(i);
			item.viewMode = AgendaView.viewMode.NONE;
			filterDataForView(item, calViewStartDate, agendaViewType);
		}
	}

}
